<?php

namespace App\Actions;

use App\Alliance\Services\MarketService;
use App\Models\Customer;
use App\Models\ExhibitorInformation;
use Encore\Admin\Actions\BatchAction;
use Encore\Admin\Admin;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class ExhibitorInformationBatchTransfer extends BatchAction
{
    public $name = '批量生成随机密码并通知';

    public function handle(Collection $collection, Request $request)
    {
        foreach ($collection as $items) {
            $exhibitor_information_id = $items->id;
            // 使用函数生成一个长度为10的随机字符串
            $uniqueRandom = $this->generateUniqueRandom(6);
            $password = Hash::make($uniqueRandom);
            $items->update(['pwd' => $uniqueRandom, 'password' => $password]);
        }

        return $this->response()->success('完成')->refresh();
    }

    function generateUniqueRandom($length = 16)
    {
        return substr(bin2hex(random_bytes($length)), 0, $length);
    }

}
